package com.example8.dynamic_datasource.config;

import com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description TODO
 * @Author lihw
 * @Date 2024/3/28 13:05
 */
@Component
public class CustomDynamicDataSourceProvider implements DynamicDataSourceProvider {

    final DataSource targetDataSource;
    final DataSource sourceDataSource;

    public CustomDynamicDataSourceProvider( @Autowired @Qualifier("primaryDataSource") DataSource targetDataSource,
                                            @Autowired @Qualifier("secondaryDataSource") DataSource sourceDataSource) {
        this.targetDataSource = targetDataSource;
        this.sourceDataSource = sourceDataSource;
    }

    @Override
    public Map<String, DataSource> loadDataSources() {
        Map<String, DataSource> dataSources = new HashMap<>();
        // 加载数据源配置并添加到dataSources映射中
        // 例如:
        // DataSource ds1 = ...; // 创建并配置DataSource
        // dataSources.put("ds1", ds1);
        // 配置动态数据源
        dataSources.put("target", targetDataSource);
        dataSources.put("source", sourceDataSource);
        return dataSources;
    }
}
